home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / misc / amag / AM95012.lha / C++-Kurs-1 / Listing2.c < prev   
Encoding:
C/C++ Source or Header  |  1994-12-20  |  1.1 KB  |  46 lines

  1. /*
  2.  * Listing2: © Clemens Marschner 1994
  3.  */
  4.  
  5. #include "listing1.c"
  6.  
  7. class Geburtsdatum : public Datum {
  8.            char *Name;
  9. public:
  10.                  Geburtsdatum(char *, int,int,int);
  11.                  // Destruktoren wird eine Tilde vorangestellt.
  12.                  // Sie haben keine Argumente und sind typenlos:
  13.                 ~Geburtsdatum();
  14.            char *GetName()  { return Name; }; // inline
  15.            void  SetName(char*);
  16.            void  Print();
  17. };
  18.  
  19. Geburtsdatum::Geburtsdatum
  20.     (char *name, int t, int m, int j) : Datum(t,m,j)
  21. {
  22.    // Konstruktor von »Datum« wird vorher aufrufen
  23.    Name = new char[strlen(name)];
  24.    // »new/delete« ersetzt malloc()/free() von C
  25.    if(Name) // noch ohne »else«, das kommt noch
  26.    {  strcpy(Name, name);  }
  27. }
  28.  
  29. Geburtsdatum::~Geburtsdatum()
  30. {  delete[] Name; // klappt auch, wenn Name == 0
  31. }
  32.  
  33. void Geburtsdatum::Print()
  34. {  cout << Name << ": ";
  35.    Datum::Print(); // Aufrufen der überschriebenen Funktion
  36. }
  37.  
  38. // ----- Und jetzt kommt das Hauptprogramm ----- //
  39. void main()
  40. {
  41.    Geburtsdatum h("Heiner", 10, 3, 48);
  42.    Geburtsdatum m("Maria", 22, 2, 53);
  43.    h.Print();   cout << " heiratet ";
  44.    m.Print();   cout << "\n";
  45. }
  46.